import datetime
from applications.extensions import db
from sqlalchemy import UniqueConstraint






class SampleRawYPModel(db.Model):
    __tablename__ = '原料_yp_取样'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    sample_dh = db.Column(db.String(255), comment='单据编号', nullable=False)
    verify_id = db.Column(db.Integer,db.ForeignKey("原料_yp_请验.id"), comment='请验_编号', nullable=False) # 属性 外键
    sample_number = db.Column(db.Numeric(16,4), comment='取样数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='取样单', comment='取样单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_sampler_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='取样人员_编号', nullable=False)
    sample_project=db.Column(db.String(255), default='药典取样通则', comment='取样方法：药典取样通则|GMP附录取样管理')
    sample_department = db.Column(db.String(255), default='仓储部', comment='取样位置：仓储部|生产车间二')
    sample_type = db.Column(db.String(255), default='正常取样', comment='取样类型:正常取样|临期取样|小样取样')
    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    verify = db.relationship('VerifyRawYPModel', foreign_keys=[verify_id], backref=db.backref('sampleRawYp'),uselist=False)
    user_sampler = db.relationship('User', foreign_keys=[user_sampler_id], backref=db.backref('sampleRawYp_sampler'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}

class SampleRawYPModelSample(db.Model):
    __tablename__ = '原料_yp_取样_小样'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    sample_dh = db.Column(db.String(255), comment='单据编号', nullable=False)
    verify_id = db.Column(db.Integer, db.ForeignKey("原料_yp_请验_小样.id"), comment='请验_编号',
                          nullable=False)  # 属性 外键

    sample_number = db.Column(db.Numeric(16,4), comment='取样数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='取样单', comment='取样单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_sampler_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='取样人员_编号', nullable=False)
    sample_project = db.Column(db.String(255), default='药典取样通则', comment='取样方法：药典取样通则|GMP附录取样管理')
    sample_department = db.Column(db.String(255), default='仓储部', comment='取样位置：仓储部|生产车间二')
    sample_type = db.Column(db.String(255), default='正常取样', comment='取样类型:正常取样|临期取样|小样取样')
    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    verify = db.relationship('VerifyRawYPModelSample', backref=db.backref('sampleRawYpSp'),uselist=False)
    user_sampler = db.relationship('User', foreign_keys=[user_sampler_id], backref=db.backref('sampleRawYp_samplerSp'),
                                   uselist=False)


    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}